{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeb490e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "840a6fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plan_template = r\"C:/users/Administrator/Desktop/xcx_plan.csv\"\n",
    "domains = r\"C:\\Share\\df\\reports\\cxxu\\0614\\20250614-xcx-网站统计情况.xlsx\"\n",
    "result_file = r\"C:/users/Administrator/Desktop/cxxu_plan.csv\"\n",
    "\n",
    "# df = pd.read_csv(plan_template, encoding=\"gbk\")  # GB18030更加强大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bab7508",
   "metadata": {},
   "outputs": [],
   "source": [
    "doms = pd.read_excel(domains)\n",
    "# 移除\"网站域名\"为空的行\n",
    "doms.dropna(how=\"all\", inplace=True)\n",
    "# 移除\"网站域名\"列中为空的行\n",
    "doms.dropna(subset=[\"网站域名\"], inplace=True)\n",
    "# doms.dropna(how=\"all\", inplace=True)\n",
    "doms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c8df82f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从doms(dataframe)中的网站域名这一列随机抽查k组(次)数据,每组数据包含3个域名\n",
    "# k = 6\n",
    "def get_domains_sample(domains=3):\n",
    "    dms = doms[\"网站域名\"].sample(domains, replace=False)\n",
    "    # dms\n",
    "    # 根据df中的表头(day,domain1,domain2,domain3,email,test_time,payment)\n",
    "    # 构造合适的字典,插入到列表中,然后将此列表合并成df对象最后输出为csv文件\n",
    "    # domain1,domain2,domain3=dms\n",
    "    return dms\n",
    "\n",
    "\n",
    "get_domains_sample()\n",
    "# 在9:00~17:30之前抽一个时间t\n",
    "import random\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "\n",
    "def get_random_time():\n",
    "    \"\"\"返回指定数量的域名字符串\"\"\"\n",
    "    # 定义起始时间和结束时间的范围（当天）\n",
    "    start_time = datetime.strptime(\"09:00\", \"%H:%M\")\n",
    "    end_time = datetime.strptime(\"17:30\", \"%H:%M\")\n",
    "\n",
    "    # 计算时间范围总秒数\n",
    "    total_seconds = int((end_time - start_time).total_seconds())\n",
    "\n",
    "    # 随机选择一个秒数\n",
    "    random_second = random.randint(0, total_seconds)\n",
    "\n",
    "    # 生成最终的随机时间\n",
    "    t = (start_time + timedelta(seconds=random_second)).strftime(\"%H:%M\")\n",
    "    # print(t)\n",
    "    return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc99810",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 编写一个\n",
    "def get_domains_sample_dict(domains=3):\n",
    "    dms_dict = {}\n",
    "    domains = get_domains_sample(domains=domains)\n",
    "    for idx, domain in enumerate(domains, start=1):\n",
    "        dms_dict[f\"domain{idx}\"] = domain\n",
    "    # print(dms_dict)\n",
    "    return dms_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a17d1a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "lines = []\n",
    "domains = 5\n",
    "for day in range(6):\n",
    "\n",
    "    line = {\n",
    "        \"day\": day + 1,\n",
    "\n",
    "        **get_domains_sample_dict(domains=domains),\n",
    "        \"email\": \"cxxu@test.com\",\n",
    "        \"test_time\": get_random_time(),\n",
    "        \"test_result\": \"\",\n",
    "    }\n",
    "    lines.append(line)\n",
    "# 将lines中的字典构造为df\n",
    "df = pd.DataFrame(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3d06748",
   "metadata": {},
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8fb288f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(result_file,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6e17de7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
